#include "Entity.h"

std::list<Entity*> 	Entity::entityList;

Entity::Entity(){
	type = 0;
	state = RUNNING;
	changed = true;
	
}
Entity::~Entity(){}
bool Entity::Initialize(unsigned int entity_number){
	type = entity_number;

	return false;
}
bool Entity::Update(){
	int x = body.GetX();
	int y = body.GetY();
	
	
	
	body.Update();
	
	
	if(x != body.GetX() || y != body.GetY()){
		SetChanged(true);
	}
	return false;
}
void Entity::Blit(){
	//virtual
	if(GlobalFunctions::DebugModeEnabled()){
		GetBody()->Blit();
	}
}
bool Entity::Deinitialize(){
	
	return true;
}
void Entity:: SetX(int x){
	body.SetX(x);
}
void Entity:: SetY(int y){
	body.SetY(y);
}
void Entity:: SetW(int w){
	body.SetW(w);
}
void Entity:: SetH(int h){
	body.SetH(h);
}
void Entity:: SetVX(float vx){
	body.SetVX(vx);
}
void Entity:: SetVY(float vy){
	body.SetVY(vy);
}
void Entity:: SetAX(float ax){
	body.SetAX(ax);
}
void Entity:: SetAY(float ay){
	body.SetAY(ay);
}
int Entity::GetX(){
	return body.GetX();
}
int Entity::GetY(){
	return body.GetY();
}
int Entity::GetW(){
	return body.GetW();
}
int Entity::GetH(){
	return body.GetH();
}
float Entity::GetVX(){
	return body.GetVX();
}
float Entity::GetVY(){
	return body.GetVY();
}
float Entity::GetAX(){
	return body.GetAX();
}
float Entity::GetAY(){
	return body.GetAY();
}
PhysicalBody* Entity::GetBody(){
	return &body;
}

unsigned int Entity::GetType(){
	return type;
}
void Entity::AddToEntityList(Entity* entity){
	entityList.push_back(entity);


}
unsigned int Entity::GetEntityCount(){
	return entityList.size();
}
//return an entity pointer
Entity* Entity::GetEntity(int number){
	if(number < 0) number = 0;
	int n = 0;
	std::list<Entity*>::iterator iter = entityList.begin();
	while(iter != entityList.end()){
		if(n == number) return (*iter);
		else{ 
			n++;
			iter++;
		}
	}
	//if not found, return last item in list
	iter--;
	return (*iter);
}
void Entity::UpdateAllEntities(){
	Entity* e;
	Entity* f;
	std::list<Entity*>::iterator i = entityList.begin();
	int i_position = 0;

	int total_entities = GetEntityCount();
	if(total_entities < 1)
		return;
	
	
    while (i != entityList.end()){	
		e = (*i);
		
		if (e->state == RUNNING){
			e->Update();
			

			if(e->GetLife() > 0)
				e->Blit();

			LibraryInterface::Print(i_position, e->GetX(), e->GetY());

			//collide against all entities in list index > this one
			for(int j = i_position + 1; j < total_entities; j++){
				f = GetEntity(j);
				e->CollideWith(f);
			}

			i++;
			i_position++;
		}
		else if( e->state == DEAD){
			delete (*i);			
			i = entityList.erase( i );	//iator = next element location
		}
		
		
    }
}

bool Entity::CollideWith(Entity* b){

	return GetBody()->CollideWith(b->GetBody());
}
void Entity::SetChanged(bool changed){
	this->changed = changed;
}
bool Entity::HasChanged(){
	return changed;
}
void Entity::ApplyForce(float x, float y){
	body.ApplyForce(x,y);
	SetChanged(true);
}
int Entity::SetLife(int life){
	return this->life = life;
}
int Entity::GetLife(){
	return life;
}